package java44th.group.service.admin.coupon;

import com.github.pagehelper.PageHelper;
import java44th.group.bean.admin.bo.coupon.CouponCreateBo;
import java44th.group.bean.admin.bo.coupon.CouponListBo;
import java44th.group.bean.admin.bo.coupon.CouponUserListBo;
import java44th.group.bean.po.MarketCoupon;
import java44th.group.bean.po.MarketCouponExample;
import java44th.group.bean.po.MarketCouponUser;
import java44th.group.bean.po.MarketCouponUserExample;
import java44th.group.bean.admin.vo.coupon.CouponListVo;
import java44th.group.bean.admin.vo.coupon.CouponUserListVo;
import java44th.group.mapper.MarketCouponMapper;
import java44th.group.mapper.MarketCouponUserMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/12 16:15
 * @Description:
 * @PACKAGE_NAME： java44th.group.service.admin.coupon
 */

@Transactional
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CouponListVo getCouponList(CouponListBo couponListBo) {
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        marketCouponExample.setDistinct(false);


        if (couponListBo.getType() != null) {
            short type = (short) (int) couponListBo.getType();
            criteria.andTypeEqualTo(type);
        }
        if (!StringUtil.isEmpty(couponListBo.getName())) {

            criteria.andNameLike("%" + couponListBo.getName() + "%");
        }
        if (couponListBo.getStatus() != null) {
            short status = (short) (int) couponListBo.getStatus();
            criteria.andStatusEqualTo(status);
        }

        int total = (int) marketCouponMapper.countByExample(marketCouponExample);

        PageHelper.startPage(couponListBo.getPage(), couponListBo.getLimit());
        marketCouponExample.setOrderByClause(couponListBo.getSort() + " " + couponListBo.getOrder());
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);

        return new CouponListVo(
                total,
                couponListBo.getPage(),
                couponListBo.getLimit(),
                total / couponListBo.getLimit() + 1,
                marketCoupons
        );


    }

    @Override
    public MarketCoupon createCoupon(CouponCreateBo couponCreateBo) {
        MarketCoupon marketCoupon =null;
        try {

            String totalStr = couponCreateBo.getTotal();
            int total = Integer.parseInt(totalStr);

//            int discountTemp = Integer.parseInt();
            double discountTemp = Double.parseDouble(couponCreateBo.getDiscount());
            BigDecimal discount = new BigDecimal(discountTemp);

            double minTemp = Double.parseDouble(couponCreateBo.getMin());
            BigDecimal min = new BigDecimal(minTemp);

            short limit = (short)(int)couponCreateBo.getLimit();
            short type = (short)(int)couponCreateBo.getType();
            short status = (short)(int)couponCreateBo.getStatus();
            short goodsType = (short)(int)couponCreateBo.getGoodsType();
            short timeType = (short)(int)couponCreateBo.getTimeType();
            short days = Short.parseShort(couponCreateBo.getDays());

            //总数小于零,满减金额小于0,最低消费小于劵的价格, 肯定有问题
            if (total < 0 || discountTemp < 0 || minTemp<discountTemp) {
                return null;
            }
            marketCoupon = new MarketCoupon(
                    null,
                    couponCreateBo.getName(),
                    couponCreateBo.getDesc(),
                    couponCreateBo.getTag(),
                    total,
                    discount,
                    min,
                    limit,
                    type,
                    status,
                    goodsType,
                    couponCreateBo.getGoodsValue().toString(),
                    couponCreateBo.getCode(),
                    timeType,
                    days,
                    couponCreateBo.getStartTime(),
                    couponCreateBo.getEndTime(),
                    new Date(),
                    new Date(),
                    false
            );
            marketCouponMapper.insertSelective(marketCoupon);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return marketCoupon;
    }

    @Override
    public MarketCoupon updateCoupon(MarketCoupon marketCoupon) {
        try {
            marketCouponMapper.updateByPrimaryKey(marketCoupon);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return marketCoupon;
    }

    @Override
    public MarketCoupon getCouponById(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public CouponUserListVo getCouponUserList(CouponUserListBo couponUserListBo) {

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        int count = (int)marketCouponUserMapper.countByExample(marketCouponUserExample);
        marketCouponUserExample.setDistinct(false);

        marketCouponUserExample.setOrderByClause(couponUserListBo.getSort() + " " + couponUserListBo.getOrder());

        if (couponUserListBo.getUserId()!=null){
            criteria.andUserIdEqualTo(couponUserListBo.getUserId());
        }
        if (couponUserListBo.getStatus()!=null){
            short status = (short)(int)couponUserListBo.getStatus();
            criteria.andStatusEqualTo(status);
        }
        if (couponUserListBo.getCouponId()!=null){
            int couponId = couponUserListBo.getCouponId();
            criteria.andCouponIdEqualTo(couponId);
        }

        PageHelper.startPage(couponUserListBo.getPage(), couponUserListBo.getLimit());
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);

        return new CouponUserListVo(
                count,
                count/couponUserListBo.getLimit()+1,
                couponUserListBo.getLimit(),
                couponUserListBo.getPage(),
                marketCouponUsers
        );

    }

    @Override
    public void deleteCouponById(int id) {
        marketCouponMapper.deleteByPrimaryKey(id);
    }


}
